package com.faxsun.core.search.service.solr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.common.SolrInputDocument;
import org.broadleafcommerce.common.locale.domain.Locale;
import org.broadleafcommerce.core.catalog.domain.Product;
import org.broadleafcommerce.core.search.domain.Field;
import org.broadleafcommerce.core.search.domain.solr.FieldType;
import org.broadleafcommerce.core.search.service.solr.SolrIndexService;
import org.broadleafcommerce.core.search.service.solr.SolrIndexServiceImpl;

public class FSSolrIndexServiceImpl extends SolrIndexServiceImpl implements SolrIndexService {

	private static final Log LOG = LogFactory.getLog(FSSolrIndexServiceImpl.class);

	@Override
	public SolrInputDocument buildDocument(final Product product, List<Field> fields, List<Locale> locales) {
		final SolrInputDocument document = new SolrInputDocument();

		attachBasicDocumentFields(product, document);

		// Keep track of searchable fields added to the index. We need to also
		// add the search facets if
		// they weren't already added as a searchable field.
		List<String> addedProperties = new ArrayList<String>();

		for (Field field : fields) {
			try {
				// Index the searchable fields
				if (field.getSearchable()) {
					List<FieldType> searchableFieldTypes = shs.getSearchableFieldTypes(field);
					for (FieldType sft : searchableFieldTypes) {
						Map<String, Object> propertyValues = getPropertyValues(product, field, sft, locales);

						// Build out the field for every prefix
						for (Entry<String, Object> entry : propertyValues.entrySet()) {
							String prefix = entry.getKey();
							prefix = StringUtils.isBlank(prefix) ? prefix : prefix + "_";

							String solrPropertyName = shs.getPropertyNameForFieldSearchable(field, sft,
									prefix);
							Object value = entry.getValue();

							// document.addField(solrPropertyName, value);
							documentAddField(document, field, solrPropertyName, value);
							addedProperties.add(solrPropertyName);
						}
					}
				}

				// Index the faceted field type as well
				FieldType facetType = field.getFacetFieldType();
				if (facetType != null) {
					Map<String, Object> propertyValues = getPropertyValues(product, field, facetType, locales);

					// Build out the field for every prefix
					for (Entry<String, Object> entry : propertyValues.entrySet()) {
						String prefix = entry.getKey();
						prefix = StringUtils.isBlank(prefix) ? prefix : prefix + "_";

						String solrFacetPropertyName = shs.getPropertyNameForFieldFacet(field, prefix);
						Object value = entry.getValue();

						if (!addedProperties.contains(solrFacetPropertyName)) {
							// document.addField(solrFacetPropertyName, value);
							documentAddField(document, field, solrFacetPropertyName, value);
						}
					}
				}
			} catch (Exception e) {
				LOG.trace("Could not get value for property[" + field.getQualifiedFieldName()
						+ "] for product id[" + product.getId() + "]", e);
			}
		}

		return document;
	}

	private void documentAddField(final SolrInputDocument document, Field field, String solrPropertyName,
			Object value) {
		if (field.getPropertyName().contains("feature")) {
			String featureStr = (String) value;
			String[] featureArray = StringUtils.split(featureStr, "|");
			
			for (String feature : featureArray) {
				document.addField(solrPropertyName, feature);
			}
		} else {
			document.addField(solrPropertyName, value);
		}
	}

}
